package public

import (
	"MoSkeleton/framework"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/global"
	"MoSkeleton/services"
	"MoSkeleton/web/pogos/response/public"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CaptchaCtrl struct {
}

// 验证码
// @Tags 公共
// @Summary 验证码
// @Description 验证码
// @Accept application/json
// @Router /public/captchaImage [get]
// @Success   200   {object}  response.Response{data=object,msg=string}  "验证码获取成功"
func (b *CaptchaCtrl) Captcha(c *gin.Context) {
	captchaProperty := &framework.ConfigContainerInst.AppProperty.App.Captcha
	enabledCaptcha := captchaProperty.Enable // 是否开启验证码
	captchaResp := public.SysCaptchaResponse{
		OpenCaptcha: enabledCaptcha,
	}
	if !enabledCaptcha {
		response.SuccessWithDataCodeAsI18n(c, consts.CaptchaClosedCode, consts.TKey_CaptchaClosedMsg, captchaResp)
		return
	}
	id, b64s, _, err := services.ServiceGroupInst.PublicGroupInst.CaptchaServiceInst.GetCaptcha()
	if err != nil {
		global.MoLogger.Error("验证码获取失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CaptchaGenFailCode, consts.TKey_CaptchaGetParamsInvalidMsg)
		return
	}
	captchaResp.CaptchaId = id
	captchaResp.PicPath = b64s
	response.SuccessAsI18n(c, consts.TKey_CaptchaGetSuccessMsg, captchaResp)
}
